﻿using Expenses.Models;
using Expenses.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Expenses.Controllers.API
{
    public class TitlesRecapController : ApiController
    {
        private ExpensesContext db = new ExpensesContext();
        // GET api/titlesrecap
        public ExpensesHome Get()
        {
            //var expensesList = db.ExpenseRecords.Where(p => p.User.UserName == User.Identity.Name).AsEnumerable();
            var titles = db.ExpenseRecords.Where(p => p.User.UserName == User.Identity.Name).GroupBy(p => p.Title).Select(p => new TitleRecap { Name = p.Key, Cost = p.Sum(c => c.Cost) });

            return new ExpensesHome
            {
                
                Titles = titles
            };
        }

        
    }
}
